package user;

import book.Book;
import book.Library;
import book.PairOfUidAndBookId;
import utils.AnalyzingBorrowedBook;

import java.util.Scanner;

import static constant.Constant.BORROW_BOOK_MAX_NUM;

public class NormalUser extends User{
    private PairOfUidAndBookId[] pairOfUidAndBookIds;
    private int borrowCount;
    private AnalyzingBorrowedBook analyzingBorrowedBook=new AnalyzingBorrowedBook();
    private Library library;
    private Scanner scanner=new Scanner(System.in);

    public NormalUser(String name,int userId){
        super(name,userId,"普通用户");
        loadBorrowedBook();
        library=Library.getInstance();
    }

    public PairOfUidAndBookId[] getPairOfUidAndBookIds() {
        return pairOfUidAndBookIds;
    }
    private void loadBorrowedBook(){
        pairOfUidAndBookIds = analyzingBorrowedBook.loadObject();
        if(pairOfUidAndBookIds == null){
            pairOfUidAndBookIds=new PairOfUidAndBookId[BORROW_BOOK_MAX_NUM];
            borrowCount=0;
            return;
        }
        borrowCount=pairOfUidAndBookIds.length;
    }

    private void storeBorrowedBook(){
        analyzingBorrowedBook.storeObject(pairOfUidAndBookIds);
    }

    public void setPairOfUidAndBookIds(PairOfUidAndBookId[] pairOfUidAndBookIds) {
        this.pairOfUidAndBookIds = pairOfUidAndBookIds;
    }

    public int getBorrowCount() {
        return borrowCount;
    }

    public void setBorrowCount(int borrowCount) {
        this.borrowCount = borrowCount;
    }

    @Override
    public int display() {
        System.out.println("普通⽤⼾ " + name + "的操作菜单:");
        System.out.println("1. 查找图书 ");
        System.out.println("2. 打印所有的图书 ");
        System.out.println("3. 退出系统 ");
        System.out.println("4. 借阅图书 ");
        System.out.println("5. 归还图书 ");
        System.out.println("6. 查看当前个⼈借阅情况 ");
        System.out.println("请选择你的操作： ");
        Scanner scanner=new Scanner(System.in);
        return scanner.nextInt();
    }



    //借阅图书
    public void borrowBook() {
        System.out.println("请输入你要借阅图书的id：");
        int bookId = scanner.nextInt();
        if(library.getBookCount()==0){
            System.out.println("书架没有书籍暂且不能借阅");
            return;
        }
        Book book = library.searchBookById(bookId);
        if(book==null){
            System.out.println("没有该id的相关书籍："+bookId);
            return;
        }

        for (int i = 0; i < borrowCount; i++) {
            PairOfUidAndBookId pairOfUidAndBookId = pairOfUidAndBookIds[i];
//找到了对应的书籍
            if (pairOfUidAndBookId.getBookId() == book.getBookId()) {
                if (getUserId() == pairOfUidAndBookId.getUserId()) {
                    System.out.println("该书已经被你借阅过了，你的ID是：" + getUserId());
                    return;
                } else {
                    System.out.println("该书已经被其他人借阅过了，他的ID是：" +
                            pairOfUidAndBookId.getUserId());
                    return;
                }
            }
        }

        library.borrowBook(bookId);
        PairOfUidAndBookId pairOfUidAndBookId = new  PairOfUidAndBookId(this.userId,bookId);
        pairOfUidAndBookIds[borrowCount] = pairOfUidAndBookId;
        borrowCount++;
        storeBorrowedBook();
        System.out.println("借阅成功！");
    }
    //归还图书
    public void returnBook() {
        loadBorrowedBook();
        if(borrowCount==0) {
            System.out.println("目前没有用户借阅过书籍");
            return;
        }
        System.out.println("请输入你要归还图书的id：");
        int bookId = scanner.nextInt();
        Book book = library.searchBookById(bookId);
        if(book == null) {
            System.out.println("没有该id的相关书籍："+bookId);
            return;
        }
        for (int i = 0; i < borrowCount; i++) {
            if(book.getBookId() == pairOfUidAndBookIds[i].getBookId()) {
                if (getUserId() == pairOfUidAndBookIds[i].getUserId()) {
                    library.returnBook(book.getBookId());
                    System.out.println("图书 '" + book.getBookName() + "' 已成功归还。");

                    pairOfUidAndBookIds[i]=pairOfUidAndBookIds[borrowCount-1];
                    pairOfUidAndBookIds[borrowCount-1] = null;
                    borrowCount--;
                    storeBorrowedBook();
                }else{
                    System.out.println("该书籍不是你借阅的书籍，不能归还："+book.getBookName());
                }
                return;
            }
        }
        System.out.println("你没有借阅该书籍，不需要归还，书籍ID为："+bookId);
    }

    //查看个⼈借阅情况
    public void viewBorrowBooks() {
        System.out.println("您的借阅情况如下：");
        if(borrowCount==0) {
            System.out.println("目前没有借阅记录.....");
        }else{
            boolean flg=false;
            for (int i = 0; i < borrowCount; i++) {
                if(pairOfUidAndBookIds[i].getUserId()==getUserId()){
                    flg=true;
                    Book book = library.searchBookById(pairOfUidAndBookIds[i].getBookId());
                    System.out.println(book);
                }

            }
        }

    }
}
